package org.kl.bf.service.basic;

import java.util.List;
import java.util.Map;

import org.kl.bf.entity.basic.NotifyContent;
import org.kl.bf.repository.basic.NotifyContentDao;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class NotifyContentService {

	private NotifyContentDao notifyContentDao;

	public NotifyContent getNotifyContent(String guid) {
		return notifyContentDao.findOne(guid);
	}

	public void saveNotifyContent(NotifyContent entity) {
		notifyContentDao.save(entity);
	}

	public void deleteNotifyContent(String guid) {
		notifyContentDao.delete(guid);
	}

	public List<NotifyContent> getAllNotifyContent() {
		return notifyContentDao.findAll();
	}

	public Page<NotifyContent> getUserNotifyContent(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<NotifyContent> spec = buildSpecification(searchParams);

		return notifyContentDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<NotifyContent> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<NotifyContent> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), NotifyContent.class);
		return spec;
	}

	@Autowired
	public void setNotifyContentDao(NotifyContentDao notifyContentDao) {
		this.notifyContentDao = notifyContentDao;
	}

}
